package br.com.atid.view.util
{
    import com.esri.ags.Map;
    import com.esri.ags.geometry.Geometry;
    import com.esri.ags.geometry.MapPoint;
    import com.esri.ags.geometry.Polyline;
    import com.esri.ags.symbol.SimpleLineSymbol;
    
    import flash.display.Sprite;

    public class ArrowLineStyle extends SimpleLineSymbol
    {

        public function ArrowLineStyle()
        {
            super();
        }

        private var m_arrowWidth:Number = 5;

        public function get arrowWidth():Number
        {
            return m_arrowWidth;
        }

        public function set arrowWidth(value:Number):void
        {
            if (value !== m_arrowWidth)
            {
                m_arrowWidth = value;
                dispatchEventChange();
            }
        }


        private var m_arrowLength:Number = 15;

        public function get arrowLength():Number
        {
            return m_arrowLength;
        }

        public function set arrowLength(value:Number):void
        {
            if (value !== m_arrowLength)
            {
                m_arrowLength = value;
                dispatchEventChange();
            }
        }

        public override function draw(sprite:Sprite, geometry:Geometry, attributes:Object, map:Map):void
        {
            if (geometry is Polyline)
            {
                sprite.graphics.lineStyle( width, color, alpha);
                const polyline:Polyline = Polyline(geometry);
                for each(var path:Array in polyline.paths)
                {
                    if (path.length > 1)
                    {
                        drawPath(sprite, path, map);
                    }
                }
            }
            super.draw(sprite, geometry, attributes, map);
        }

        private function drawPath(sprite:Sprite, path:Array, map:Map):void
        {
            var x:Number, y:Number;

            const p1:MapPoint = path[path.length - 1];

            const p2:MapPoint = path[path.length - 2];

            const x1:Number = toScreenX(map, p1.x) - sprite.x;
            const y1:Number = toScreenY(map, p1.y) - sprite.y;
            const x2:Number = toScreenX(map, p2.x) - sprite.x;
            const y2:Number = toScreenY(map, p2.y) - sprite.y;

            const ang:Number = -Math.atan2(x2 - x1, y2 - y1);
            const cos:Number = Math.cos(ang);
            const sin:Number = Math.sin(ang);

            const ax:Number = x1;
            const ay:Number = y1;
            x = -m_arrowWidth;
            y = m_arrowLength;
            const bx:Number = x * cos - y * sin + x1;
            const by:Number = x * sin + y * cos + y1;
            x = m_arrowWidth;
            y = m_arrowLength;
            const cx:Number = x * cos - y * sin + x1;
            const cy:Number = x * sin + y * cos + y1;

            sprite.graphics.beginFill(color, alpha);
            sprite.graphics.moveTo(ax, ay);
            sprite.graphics.lineTo(bx, by);
            sprite.graphics.lineTo(cx, cy);
            sprite.graphics.lineTo(ax, ay);
            sprite.graphics.endFill();
        }
    }
}